//
//  HJSelectMemberView.m
//  KaiChat
//
//  Created by lx on 2023/11/2.
//

#import "HJSelectMemberView.h"
#import "HJCollectionViewCell.h"
#import "HJChatFriendModel.h"

@interface HJSelectMemberViewCell: HJCollectionViewCell

@property (nonatomic, strong) HJChatFriendModel *model;
@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UIButton *deleteButton;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, copy) void (^didClickDeleteButtonBlock)(void);


@end

@implementation HJSelectMemberViewCell


- (void)settingSubView {
    [super settingSubView];

    UIImageView *avatarImageView = [UIImageView addToView:self.contentView];
    avatarImageView.addCornerRadius(20);
    avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(6);
        make.centerX.offset(0);
        make.width.height.offset(40);
    }];
 
    UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentCenter];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(avatarImageView.mas_bottom).offset(4);
    }];
    
    UIButton *deleteButton = [UIButton addToView:self.contentView];
    [deleteButton setBackgroundImage:[UIImage imageNamed:@"select_delete"] forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(didClickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(avatarImageView);
        make.top.equalTo(avatarImageView);
        make.width.height.offset(14);
    }];
    
    _deleteButton = deleteButton;
    _avatarImageView = avatarImageView;
    _nameLabel = nameLabel;
}

- (void)setModel:(HJChatFriendModel *)model {
    _model = model;
    
    _nameLabel.text = model.showName;
    if (isEmptyString(model.avatar)) {
        _avatarImageView.image = [UIImage imageNamed:defaultAvatarName];
    } else {
        [_avatarImageView sd_setImageWithURL:model.avatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    }
}


- (void)didClickDeleteButton {
    if (_didClickDeleteButtonBlock) {
        _didClickDeleteButtonBlock();
    }
}

@end

@interface HJSelectMemberView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@end


@implementation HJSelectMemberView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.offset(0);
        }];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJSelectMemberViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HJSelectMemberViewCell" forIndexPath:indexPath];
    HJChatFriendModel *model = self.datas[indexPath.row];
    cell.model = model;
    TJWeakify(self)
    [cell setDidClickDeleteButtonBlock:^{
        if (weakself.didClickDeleteButtonBlock) {
            weakself.didClickDeleteButtonBlock(model);
        }
    }];
    return cell;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout = flowLayout;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, leftRightMargin, 0, leftRightMargin);
        flowLayout.itemSize = CGSizeMake(64, 70);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:HJSelectMemberViewCell.class forCellWithReuseIdentifier:@"HJSelectMemberViewCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.alwaysBounceHorizontal=YES;
        
    }
    return _collectionView;
}


- (void)setDatas:(NSMutableArray *)datas {
    _datas = datas;
    
    [self.collectionView reloadData];
}

- (void)reloadData {
    [self.collectionView reloadData];
}
- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
